package com.example.administrator.materialdesign;

//import android.app.Fragment;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*添加第一个碎片*/
        replaceFragment(new oneFragment());


        Toolbar toolbar=(Toolbar) findViewById(R.id.main_toobar_1);
        setSupportActionBar(toolbar);

        drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        NavigationView navigationView=(NavigationView) findViewById(R.id.nav_view);
        BottomNavigationBar mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        ActionBar actionBar=getSupportActionBar();    //获取actionbar的实例
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);              //让导航按钮显示出来
            actionBar.setHomeAsUpIndicator(R.mipmap.menu );             //设置导航按钮图标
        }
        /*侧滑菜单点击事件*/
        navigationView.setCheckedItem(R.id.nav_call);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
            public boolean onNavigationItemSelected(MenuItem item){
                drawerLayout.closeDrawers();        //点击任何一项的时候，关闭侧滑栏
                /*点击对应的菜单式执行不同的操作*/
                switch (item.getItemId()){
                    case R.id.nav_call:
                        Toast.makeText(MainActivity.this, "您点击了拨打电话!", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_friends:
                        Intent intent=new Intent(MainActivity.this,LoginActivity.class);            //跳转到登录界面
                        startActivity(intent);
                        break;
                    case R.id.nav_location:
                    {Toast.makeText(MainActivity.this, "您点击了位置", Toast.LENGTH_SHORT).show();}
                        break;
                    default:
                }
                return true;
            }
        });



        /*底部菜单栏*/


        BadgeItem badge=new BadgeItem()
//                .setBorderWidth(2)//Badge的Border(边界)宽度
                .setBorderColor("#FFFFFF")//Badge的Border颜色
//                .setBackgroundColor("#FFFFFF")//Badge背景颜色
//                .setGravity(Gravity.RIGHT| Gravity.TOP)//位置，默认右上角
                .setText("2");//显示的文本
//                .setTextColor("#F0F8FF")//文本颜色
//                .setAnimationDuration(2000)
//                .setHideOnSelect(true)//当选中状态时消失，非选中状态显示


        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.icon_one, "视频").setActiveColorResource(R.color.green).setBadgeItem(badge))
                .addItem(new BottomNavigationItem(R.drawable.icon_two, "音乐").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.icon_three, "新闻").setActiveColorResource(R.color.lime))
                .addItem(new BottomNavigationItem(R.drawable.icon_four, "个人").setActiveColorResource(R.color.lime))//依次添加item,分别icon和名称
                .setFirstSelectedPosition(0)//设置默认选择item
                .initialise();//初始化

        /*底部导航栏的事件监听*/
        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {//这里也可以使用SimpleOnTabSelectedListener
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                switch (position) {
                    case 0:
                       replaceFragment(new oneFragment());      //切换碎片
                        break;
                    case 1:
                       replaceFragment(new towfragment());        //切换碎片
                        break;
                    case 2:
                      replaceFragment(new three());
                        break;
                    case 3:
                       replaceFragment(new fourFragment());
                        break;
                    default:

                        break;
                }

            }

            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中
            }

            @Override
            public void onTabReselected(int position) {//选中 -> 选中
            }
        });


    }



    /*动态添加碎片的函数*/
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment,fragment);
        fragmentTransaction.commit();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.backup:
                Toast.makeText(this, "你点击了上传", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "你点击删除按钮", Toast.LENGTH_SHORT).show();
                break;
            case R.id.settion:
                Toast.makeText(this, "你点击了设置按钮", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
